//
//  RaeNetworkResult.swift
//  77Club
//
//  Created by Apple on 2024/8/26.
//

import UIKit
import KakaJSON

//TODO: - 网络结果返回
enum RaeNetworkResult<Base> {
    // 请求成功
    case success(_ models: [Base]?)
    // 请求失败
    case failure(_ error: RaeNetworkError)
    
    var isSuccess: Bool {
        switch self {
        case .success:
            return true
        default:
            return false
        }
    }
    
    var model: Base? {
        switch self {
        case .success(let models):
            return models?.first
        default:
            return nil
        }
    }
    
    var models: [Base]? {
        switch self {
        case .success(let models):
            return models
        default:
            return nil
        }
    }
    
    var errorCode: Int? {
        switch self {
        case .failure(let error):
            return error.code
        default:
            return nil
        }
    }
    
    var errorMessage: String? {
        switch self {
        case .failure(let error):
            return error.message
        default:
            return nil
        }
    }
    
}

enum RaeNetworkError {
    case error(code: Int, error: String)
    
    var code: Int {
        switch self {
        case .error(let code, _):
            return code
        }
    }
    
    var message: String {
        switch self {
        case .error(_, let error):
            return error
        }
    }
}

//TODO: - 网络结果基本模型
struct RaeNetworkBaseModel: Convertible {
    var code: Int           = 0
    var message: String     = ""
//    var data:               [String: Any]?
    var data:                Any?
    
    mutating func kj_didConvertToModel(from json: [String: Any]) {
        if let arrayData = json["data"] as? [[String: Any]] {
            self.data = arrayData
        } else if let dictData = json["data"] as? [String: Any] {
            self.data = dictData
        }
    }
}

